//
//  ViewController.m
//  aMoment
//
//  Created by dllo on 15/9/30.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "ViewController.h"
#import "AllHeaderFiles.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, assign)NSInteger start;
@property(nonatomic, retain)MBProgressHUD *progressView;
@end

@implementation ViewController

- (void)dealloc
{
    [_tableView release];
    [_progressView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"首页";
    self.dataSourceArray = [NSMutableArray array];
    [self getData];
    
    _tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _start = 0;
        [self getData];
    }];
//    [_tableView.header beginRefreshing];
    _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _start += 10;
        [self getData];
    }];
}

#pragma mark 获取数据
- (void)getData
{
    NSString *bodyString = [NSString stringWithFormat:@"start=%ld&limit=10&client=2", _start];
    [NetworkRequest getDataWithURLString:URLSTRING_HOME BodyString:bodyString DataBlock:^(id data) {
        //移除加载中提示视图
        [_progressView removeFromSuperview];
        
        if (!_start) {
            [self.dataSourceArray removeAllObjects];
        }
        NSArray *listArray = [[(NSDictionary *)data objectForKey:@"data"] objectForKey:@"list"];
        for (NSDictionary *dict in listArray) {
            Home *home = [[Home alloc] init];
            [home setValuesForKeysWithDictionary:dict];
            [self.dataSourceArray addObject:home];
        }
        [_tableView reloadData];
        [_tableView.header endRefreshing];
        [_tableView.footer endRefreshing];
    }];
}

- (void)loadView
{
    [super loadView];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
    
    //创建加载中提示视图
    self.progressView = [LoadingProgress createViewWithSuperviewFrame:self.view.frame];
    [self.view addSubview:_progressView];
    [_progressView release];
}

#pragma mark cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArray.count;
}

#pragma mark cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.dataSourceArray[indexPath.row] enname] isEqualToString:@"Timeline"]) {
        static NSString *identifier = @"Timeline";
        HomeTableCellOfTimeLine *timelineCell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!timelineCell) {
            timelineCell = [[HomeTableCellOfTimeLine alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        timelineCell.selectionStyle = UITableViewCellSelectionStyleNone;
        timelineCell.home = self.dataSourceArray[indexPath.row];
        return timelineCell;
    }
    if ([[self.dataSourceArray[indexPath.row] enname] isEqualToString:@"Sound"]) {
        static NSString *identifier = @"Sound";
        HomeTableCellOfSound *soundCell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!soundCell) {
            soundCell = [[HomeTableCellOfSound alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        soundCell.selectionStyle = UITableViewCellSelectionStyleNone;
        soundCell.home = self.dataSourceArray[indexPath.row];
        return soundCell;
    }
    if ([[self.dataSourceArray[indexPath.row] enname] isEqualToString:@"Music"]) {
        static NSString *identifier = @"Music";
        HomeTableCellOfMusic *musicCell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!musicCell) {
            musicCell = [[HomeTableCellOfMusic alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        musicCell.selectionStyle = UITableViewCellSelectionStyleNone;
        musicCell.home = self.dataSourceArray[indexPath.row];
        return musicCell;
    }
    if ([[self.dataSourceArray[indexPath.row] enname] isEqualToString:@"Photo"] || [[self.dataSourceArray[indexPath.row] enname] isEqualToString:@"Illustration"]) {
        static NSString *identifier = @"Photo";
        HomeTableCellOfPhoto *photoCell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!photoCell) {
            photoCell = [[HomeTableCellOfPhoto alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        photoCell.selectionStyle = UITableViewCellSelectionStyleNone;
        photoCell.home = self.dataSourceArray[indexPath.row];
        return photoCell;
    }
    static NSString *identifier = @"table";
    HomeTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[HomeTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.home = self.dataSourceArray[indexPath.row];
    return cell;
}

#pragma mark cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.dataSourceArray[indexPath.row] enname] isEqualToString:@"Timeline"]) {
        return [self getStringHeightWithString:[self.dataSourceArray[indexPath.row] content] LabelWidth:(SCREEN_WIDTH - 50 * FIT_WIDTH - IMAGE_16 * 2)] + 341 * FIT_HEIGHT;
    }
    if ([[self.dataSourceArray[indexPath.row] enname] isEqualToString:@"Sound"]) {
        return 301 * FIT_HEIGHT;
    }
    if ([[self.dataSourceArray[indexPath.row] enname] isEqualToString:@"Music"]) {
        return [self getStringHeightWithString:[self.dataSourceArray[indexPath.row] content] LabelWidth:(SCREEN_WIDTH - 40 * FIT_WIDTH)] + 135 * FIT_HEIGHT;
    }
    if ([[self.dataSourceArray[indexPath.row] enname] isEqualToString:@"Photo"] || [[self.dataSourceArray[indexPath.row] enname] isEqualToString:@"Illustration"]) {
        return [self getStringHeightWithString:[self.dataSourceArray[indexPath.row] content] LabelWidth:(SCREEN_WIDTH - 40 * FIT_WIDTH)] + (155 * FIT_HEIGHT + 330 * FIT_WIDTH);
    }
    return [self getStringHeightWithString:[self.dataSourceArray[indexPath.row] content] LabelWidth:(SCREEN_WIDTH - 40 * FIT_WIDTH)] + 305 * FIT_HEIGHT;
}

#pragma mark 获取字符串高度
- (CGFloat)getStringHeightWithString:(NSString *)string
                          LabelWidth:(CGFloat)labelWidth
{
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:FONTSIZE_MIDDLE], NSFontAttributeName, nil];
    CGRect rect = [string boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    return rect.size.height * FIT_HEIGHT + 1;
}

#pragma mark cell点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *enname = [self.dataSourceArray[indexPath.row] enname];
    if ([enname isEqualToString:@"Timeline"]) {
        TimelineDetailsViewController *timelineDetailsVC = [[TimelineDetailsViewController alloc] init];
        [self.navigationController pushViewController:timelineDetailsVC animated:YES];
        timelineDetailsVC.contentId = [self.dataSourceArray[indexPath.row] ID];
        [timelineDetailsVC release];
        return;
    }
    if ([enname isEqualToString:@"Sound"]) {
        Home *home = self.dataSourceArray[indexPath.row];
        RadioDetailsInfoViewController *radioDetailsInfoVC = [[RadioDetailsInfoViewController alloc] init];
        radioDetailsInfoVC.tingId = [home.playList.firstObject objectForKey:@"tingid"];
        radioDetailsInfoVC.listArray = home.playList;
        [self playMusic:home];
        
        
        //播放
//        [[MusicSingleton shareInstance] playMusicWith:[infoVC.listArray[0] objectForKey:@"musicUrl"]];
//        RadioDetails *details = [[RadioDetails alloc] init];
//        details.musicUrl = [infoVC.listArray[0] objectForKey:@"musicUrl"];
//        details.coverimg = [infoVC.listArray[0] objectForKey:@"imgUrl"];
//        details.title = [infoVC.listArray[0] objectForKey:@"title"];
//        details.playInfo = [self.dataSourceArray[indexPath.row] playInfo];
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"music" object:details];
        
        [self.navigationController pushViewController:radioDetailsInfoVC animated:YES];
        [radioDetailsInfoVC release];
        return;
    }
    if ([enname isEqualToString:@"Music"] || [enname isEqualToString:@"Photo"] || [enname isEqualToString:@"Illustration"] || [enname isEqualToString:@"Topic"]) {
        StoreDetailsViewController *storeDetailsVC = [[StoreDetailsViewController alloc] init];
        [self.navigationController pushViewController:storeDetailsVC animated:YES];
        storeDetailsVC.contentID = [self.dataSourceArray[indexPath.row] ID];
        [storeDetailsVC release];
        return;
    }
    HomeDetailsViewController *homeDetailsVC = [[HomeDetailsViewController alloc] init];
    [self.navigationController pushViewController:homeDetailsVC animated:YES];
    homeDetailsVC.contentId = [self.dataSourceArray[indexPath.row] ID];
    [homeDetailsVC release];
    return;
}

#pragma mark 播放音乐
- (void)playMusic:(Home *)home
{
    Music *music = [[Music alloc] init];
    music.musicURL = [home.playList.firstObject objectForKey:@"musicUrl"];
    music.coverImageURL = [home.playList.firstObject objectForKey:@"imgUrl"];
    music.songName = [home.playList.firstObject objectForKey:@"title"];
    music.singer = [[home.playList.firstObject objectForKey:@"userinfo"] objectForKey:@"uname"];
    music.tingId = [home.playList.firstObject objectForKey:@"tingid"];
    
    //播放音乐
    [[MusicSingleton shareInstance] playMusicWith:music.musicURL];
    
    //改变音乐播放条详情
    [[MusicSingleton shareInstance] setMusicModel:music];
}

#pragma mark 改变时间条字体颜色
-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
